package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {
        List<SpecGroup> specGroups = this.specService.querySpecGroups(cid);
        if (specGroups != null || 0 != specGroups.size()) {
            return ResponseEntity.ok(specGroups);
        }
        return ResponseEntity.status(204).build();
    }


    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParam(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching,
            @RequestParam(value = "generic", required = false) Boolean generic) {
        List<SpecParam> specParams = this.specService.querySpecParam(gid, cid, searching, generic);
        if (specParams != null || 0 != specParams.size()) {
            return ResponseEntity.ok(specParams);
        }
        return ResponseEntity.status(204).build();
    }


    @PutMapping("group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroup group) {
        this.specService.updateGroup(group);
        return ResponseEntity.status(201).build();//返回201表示修改完成
    }

    @PostMapping("group")
    public ResponseEntity<Void> insertGroup(@RequestBody SpecGroup group) {
        this.specService.insertGroup(group);
        return ResponseEntity.status(201).build();//返回201表示修改完成
    }

}
